package com.th.supcom.pds.config.client.widget;

import java.util.List;

import net.carefx.framework.container.client.context.AppContext;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.config.client.common.PdsConfigConstant;
import com.th.supcom.pds.config.client.common.PhamConfigServiceFactory;
import com.th.supcom.pds.config.client.common.PhamWindowConfigModuleEvents;
import com.th.supcom.pds.config.client.model.PhamWindowConfigModel;
import com.th.supcom.pds.config.client.service.PhamWindowConfigRpcAsync;

/**
 * @author 王剑
 */
public class PhamWindowConfigAction
{
    private static final PhamWindowConfigRpcAsync S_PHAM_WINDOW_CONFIG_RPC_ASYNC = PhamConfigServiceFactory.getPhamWindowConfigService ();

    /**
     * 删除记录
     * 
     * @param configModel
     */
    private void doRemovePhamDispensaryConfig (final PhamWindowConfigModel configModel,
                                               final PhamWindowConfigMainPanel panel)
    {
        MessageBox.confirm ("", "确定删除【" + configModel.getWindowName () + "】属性配置吗？", new Listener <MessageBoxEvent> ()
        {
            @Override
            public void handleEvent (final MessageBoxEvent be)
            {
                verifyDelete (configModel, be);
            }

            private void verifyDelete (final PhamWindowConfigModel configModel, final MessageBoxEvent be)
            {
                if (Dialog.YES.equals (be.getButtonClicked ().getItemId ()))
                {
                    PhamWindowConfigAction.S_PHAM_WINDOW_CONFIG_RPC_ASYNC.removePhamWindowConfig (configModel.getPhamWindowConfigId (),
                                                                                                  new AsyncCallback <Void> ()
                                                                                                  {
                                                                                                      @Override
                                                                                                      public void onFailure (final Throwable caught)
                                                                                                      {
                                                                                                          MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                            caught.getMessage (),
                                                                                                                            null);
                                                                                                      }

                                                                                                      @Override
                                                                                                      public void onSuccess (final Void result)
                                                                                                      {
                                                                                                          Info.display (PdsConfigConstant.INFO,
                                                                                                                        "删除药房窗口属性成功");
                                                                                                          fireSearchEvent (panel);
                                                                                                      }
                                                                                                  });
                }
            }
        });
    }

    /**
     * 编辑药房窗口属性
     * 
     * @param configModel
     */
    private void editPhamWindowConfig (final EditPhamWindowConfigWindow editWindow,
                                       final PhamWindowConfigMainPanel panel)
    {
        editWindow.addListener (PhamWindowConfigModuleEvents.UI_REFRESH, new Listener <BaseEvent> ()
        {

            @Override
            public void handleEvent (final BaseEvent be)
            {
                refreshData (getSearchCondition (panel), panel);
            }
        });
    }

    public void enableBtnListener (final PhamWindowConfigModel updateModel, final PhamWindowConfigMainPanel panel)
    {
        PhamWindowConfigAction.S_PHAM_WINDOW_CONFIG_RPC_ASYNC.modifyPhamWindowConfigByExample (updateModel,
                                                                                               new AsyncCallback <Void> ()
                                                                                               {

                                                                                                   @Override
                                                                                                   public void onFailure (final Throwable caught)
                                                                                                   {
                                                                                                       MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                         caught.getMessage (),
                                                                                                                         null);
                                                                                                   }

                                                                                                   @Override
                                                                                                   public void onSuccess (final Void result)
                                                                                                   {
                                                                                                       Info.display (PdsConfigConstant.INFO,
                                                                                                                     "更新药房窗口属性成功");
                                                                                                       fireSearchEvent (panel);
                                                                                                   }
                                                                                               });
    }

    public final void fireSearchEvent (final PhamWindowConfigMainPanel panel)
    {
        final PhamWindowConfigModel model = getSearchCondition (panel);
        final AppEvent appEvent = new AppEvent (PhamWindowConfigModuleEvents.UI_REFRESH, model);
        panel.fireEvent (PhamWindowConfigModuleEvents.UI_REFRESH, appEvent);
    }

    private PhamWindowConfigModel getSearchCondition (final PhamWindowConfigMainPanel panel)
    {
        final PhamWindowConfigModel model = new PhamWindowConfigModel ();
        // 根据控制面板获取查询条件
        String deptCode =ClientUtils.getOrgModel().getOrgCode();
        model.setDeptCode (deptCode);
        return model;
    }

    /**
     * 获取选中行
     * 
     * @return
     */
    private PhamWindowConfigModel getSelectedModel (final PhamWindowConfigMainPanel panel)
    {
        final PhamWindowConfigModel configModel = panel.getWindowGrid ().getSelectionModel ().getSelectedItem ();
        if (configModel == null)
        {
            MessageBox.alert (PdsConfigConstant.INFO, "请先选择记录！", null);
        }
        return configModel;
    }

    /**
     * 刷新数据
     * 
     * @param configModel
     */
    private void refreshData (final PhamWindowConfigModel configModel, final PhamWindowConfigMainPanel panel)
    {
        panel.getWindowGrid ().mask (PdsConfigConstant.GRID_DEFAULT_LOAD_MESSAGE);
        PhamWindowConfigAction.S_PHAM_WINDOW_CONFIG_RPC_ASYNC.findPhamWindowConfigByExample (configModel,
                                                                                             new AsyncCallback <List <PhamWindowConfigModel>> ()
                                                                                             {
                                                                                                 @Override
                                                                                                 public void onFailure (final Throwable caught)
                                                                                                 {
                                                                                                     panel.getWindowGrid ()
                                                                                                          .unmask ();
                                                                                                     MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                       caught.getMessage (),
                                                                                                                       null);
                                                                                                 }

                                                                                                 @Override
                                                                                                 public void onSuccess (final List <PhamWindowConfigModel> result)
                                                                                                 {
                                                                                                     panel.getListStore ()
                                                                                                          .removeAll ();
                                                                                                     panel.getListStore ()
                                                                                                          .add (result);
                                                                                                     panel.getWindowGrid ()
                                                                                                          .unmask ();
                                                                                                 }
                                                                                             });
    }

    /**
     * 注册监听事件.
     * 
     */
    public void registerListeners (final PhamWindowConfigMainPanel panel)
    {
        panel.addListener (PhamWindowConfigModuleEvents.UI_REFRESH, new Listener <AppEvent> ()
        {

            @Override
            public void handleEvent (final AppEvent be)
            {
                final PhamWindowConfigModel model = be.getData ();
                refreshData (model, panel);
            }
        });
        panel.getRefreshButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                fireSearchEvent (panel);
            }
        });
        panel.getEditButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final PhamWindowConfigModel configModel = getSelectedModel (panel);
                final EditPhamWindowConfigWindow editWindow = new EditPhamWindowConfigWindow (configModel);
                editWindow.setHeading ("编辑药房窗口属性");
                editWindow.show ();
                editPhamWindowConfig (editWindow, panel);
            }

        });
        panel.getAddButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {    
                
                final PhamWindowConfigModel configModel = getLocalModel ();
                final EditPhamWindowConfigWindow editWindow = new EditPhamWindowConfigWindow (configModel);
                editWindow.setHeading ("新增药房窗口属性");
                editWindow.show ();
                editPhamWindowConfig (editWindow, panel);
            }
        });
        panel.getRemoveButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final PhamWindowConfigModel configModel = getSelectedModel (panel);
                doRemovePhamDispensaryConfig (configModel, panel);
            }
        });
        panel.getWindowGrid ().addListener (Events.CellDoubleClick, new Listener <GridEvent <PhamWindowConfigModel>> ()
        {

            @Override
            public void handleEvent (final GridEvent <PhamWindowConfigModel> be)
            {
                final EditPhamWindowConfigWindow editWindow = new EditPhamWindowConfigWindow (be.getModel ());
                editWindow.setHeading ("编辑药房窗口属性");
                editWindow.show ();
                editPhamWindowConfig (editWindow, panel);
            }
        });
        this.refreshData (getSearchCondition (panel), panel);
        new KeyNav <ComponentEvent> (panel)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                if (ce.isAltKey ())
                {
                    shortCutKey (ce.getKeyCode (), panel);
                }
            }
        };
        refreshData (getSearchCondition (panel), panel);
    }

    /**
     * 快捷键
     * 
     * @param keyCode
     */
    private void shortCutKey (final int keyCode, final PhamWindowConfigMainPanel panel)
    {
        if (keyCode == KeysConstants.VK_D)
        {
            final PhamWindowConfigModel configModel = getSelectedModel (panel);
            doRemovePhamDispensaryConfig (configModel, panel);
        }
        if (keyCode == KeysConstants.VK_F)
        {
            fireSearchEvent (panel);
        }
        if (keyCode == KeysConstants.VK_A)
        {
            final EditPhamWindowConfigWindow editWindow = new EditPhamWindowConfigWindow ();
            editWindow.setHeading ("编辑药房窗口属性");
            editWindow.show ();
            editPhamWindowConfig (editWindow, panel);
        }
        if (keyCode == KeysConstants.VK_X)
        {
            final PhamWindowConfigModel configModel = getSelectedModel (panel);
            final EditPhamWindowConfigWindow editWindow = new EditPhamWindowConfigWindow (configModel);
            editWindow.setHeading ("编辑药房窗口属性");
            editWindow.show ();
            editPhamWindowConfig (editWindow, panel);
        }
    }
    private PhamWindowConfigModel getLocalModel ()
    {
        final PhamWindowConfigModel configModel =new PhamWindowConfigModel();
        configModel.setDeptCode (ClientUtils.getOrgModel().getOrgCode());
        configModel.setDeptName (ClientUtils.getOrgModel().getOrgName());
        return configModel;
    }
}
